package www.ld.com.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import www.ld.com.core.group.InsertGroup;
import www.ld.com.core.group.UpdateGroup;
import www.ld.com.domain.R;
import www.ld.com.domain.attr.group.pojo.AttrGroupPO;
import www.ld.com.domain.attr.group.query.AttrGroupQuery;
import www.ld.com.domain.attr.group.vo.AttrGroupVO;
import www.ld.com.domain.attr.group.vo.InAttrGroupVO;
import www.ld.com.domain.attr.group.vo.OutAttrGroupVO;
import www.ld.com.domain.attr.relation.vo.InRelation;
import www.ld.com.domain.vo.PageRestVO;
import www.ld.com.service.AttrGroupService;
import www.ld.com.utils.PageUtils;

import javax.annotation.Resource;
import java.util.Set;

@Api(tags = "属性分组")
@RestController
@RequestMapping("/attr-group")
public class AttrGroupController {

    @Resource
    private AttrGroupService attrGroupService;

    @GetMapping
    @ApiOperation(value = "属性分组列表")
    @PreAuthorize("hasAuthority('pms:attr-group:select')")
    public R<PageRestVO<AttrGroupVO>> pageList(AttrGroupQuery attrGroupQuery) {
        PageRestVO pageRestVO = PageUtils.copyPageVO(attrGroupService.pageAttrGroupByAttrGroupQuery(attrGroupQuery), AttrGroupVO.class);
        return R.ok(pageRestVO);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "获取属性分组详情")
    @PreAuthorize("hasAuthority('pms:attr-group:select')")
    public R<OutAttrGroupVO> getAttrGroupById(@PathVariable(value = "id", required = true) String id) {
        AttrGroupPO attrGroupPO = attrGroupService.getAttrGroupPOById(id);
        return R.ok(BeanUtil.toBean(attrGroupPO, OutAttrGroupVO.class));
    }

    @ApiOperation(value = "新增属性分组")
    @PostMapping
    @PreAuthorize("hasAuthority('pms:attr-group:create')")
    public R save(@RequestBody @Validated(value = {InsertGroup.class}) InAttrGroupVO inAttrGroupVO) {
        attrGroupService.saveAttrGroupPO(BeanUtil.toBean(inAttrGroupVO, AttrGroupPO.class));
        return R.ok();
    }

    @ApiOperation(value = "修改属性分组")
    @PutMapping
    @PreAuthorize("hasAuthority('pms:attr-group:update')")
    public R update(@RequestBody @Validated(value = {UpdateGroup.class}) InAttrGroupVO inAttrGroupVO) {
        attrGroupService.updateAttrGroupPO(BeanUtil.toBean(inAttrGroupVO, AttrGroupPO.class));
        return R.ok();
    }

    @ApiOperation(value = "删除属性分组")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('pms:attr-group:delete')")
    public R delete(@PathVariable(value = "id", required = true) String id) {
        attrGroupService.deleteAttrGroupById(id);
        return R.ok();
    }


    @ApiOperation(value = "关联属性分组")
    @PostMapping("/relation")
    @PreAuthorize("hasAuthority('pms:attr-group:relation')")
    public R relation(@RequestBody @Validated InRelation inRelation) {
        attrGroupService.relation(inRelation);
        return R.ok();
    }


    @ApiOperation(value = "获取关联属性分组")
    @GetMapping("/relation/{attrGroupId}")
    @PreAuthorize("hasAuthority('pms:attr-group:relation')")
    public R<Set<String>> getRelation(@PathVariable(value = "attrGroupId", required = true) String attrGroupId) {
        return R.ok(attrGroupService.getRelation(attrGroupId));
    }
}
